package model;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class SaleStatistics implements Serializable {
    private List<Ticket> tickets;

    public SaleStatistics() {
        this.tickets = new ArrayList<>();
    }

    public void addTicket(Ticket ticket) {
        tickets.add(ticket);
    }

    public List<Ticket> getAllTickets() {
        return new ArrayList<>(tickets);
    }

    public List<Ticket> getTicketsByDate(LocalDate date) {
        return tickets.stream()
                .filter(ticket -> ticket.getSaleTime().toLocalDate().equals(date))
                .collect(Collectors.toList());
    }

    public List<Ticket> getTicketsForWeek(LocalDate startDate) {
        LocalDate endDate = startDate.plusDays(6);
        return tickets.stream()
                .filter(ticket -> {
                    LocalDate ticketDate = ticket.getSaleTime().toLocalDate();
                    return (ticketDate.isEqual(startDate) || ticketDate.isAfter(startDate)) 
                           && (ticketDate.isEqual(endDate) || ticketDate.isBefore(endDate));
                })
                .collect(Collectors.toList());
    }

    public List<Ticket> getTicketsForMonth(int year, int month) {
        return tickets.stream()
                .filter(ticket -> {
                    LocalDate ticketDate = ticket.getSaleTime().toLocalDate();
                    return ticketDate.getYear() == year && ticketDate.getMonthValue() == month;
                })
                .collect(Collectors.toList());
    }

    // 统计有效售票数量
    public int getValidTicketCount(List<Ticket> ticketList) {
        return (int) ticketList.stream()
                .filter(ticket -> ticket.getStatus() == TicketStatus.VALID || ticket.getStatus() == TicketStatus.USED)
                .count();
    }

    // 统计有效售票金额
    public double getValidTicketAmount(List<Ticket> ticketList) {
        return ticketList.stream()
                .filter(ticket -> ticket.getStatus() == TicketStatus.VALID || ticket.getStatus() == TicketStatus.USED)
                .mapToDouble(Ticket::getPrice)
                .sum();
    }

    // 统计退票数量
    public int getRefundedTicketCount(List<Ticket> ticketList) {
        return (int) ticketList.stream()
                .filter(ticket -> ticket.getStatus() == TicketStatus.REFUNDED)
                .count();
    }

    // 统计退票金额
    public double getRefundedTicketAmount(List<Ticket> ticketList) {
        return ticketList.stream()
                .filter(ticket -> ticket.getStatus() == TicketStatus.REFUNDED)
                .mapToDouble(Ticket::getPrice)
                .sum();
    }

    // 统计总票价
    public double getTotalAmount(List<Ticket> ticketList) {
        return getValidTicketAmount(ticketList) - getRefundedTicketAmount(ticketList);
    }

    // 根据场次ID获取票
    public List<Ticket> getTicketsByScreeningId(String screeningId) {
        return tickets.stream()
                .filter(ticket -> ticket.getScreening().getId().equals(screeningId))
                .collect(Collectors.toList());
    }
} 